package com.grass.p019lv.novel.fragment;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.BaseSdk;
import com.androidx.p013lv.base.glide.GlideUtils;
import com.androidx.p013lv.base.utils.ButtonUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.TimeUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.base.view.CircleImageView;
import com.grass.p019lv.bean.NovelReplyBean;
import com.like.LikeButton;
import com.like.OnLikeListener;
import java.util.ArrayList;
import java.util.List;

/* renamed from: com.grass.lv.novel.fragment.NovelCommentVerticalLayout */
public class NovelCommentVerticalLayout extends LinearLayout implements ViewGroup.OnHierarchyChangeListener {
    private int mCommentVerticalSpace;
    private LinearLayout.LayoutParams mLayoutParams;
    private OnTwoClickListener onTwoClickListener;
    private String parentId;
    private List<NovelReplyBean> replyList;
    private int userId;

    /* renamed from: com.grass.lv.novel.fragment.NovelCommentVerticalLayout$OnTwoClickListener */
    public interface OnTwoClickListener {
        void onTwoClick(NovelReplyBean novelReplyBean, String str);
    }

    public void onChildViewAdded(View view, View view2) {
    }

    public void onChildViewRemoved(View view, View view2) {
    }

    public void setParentId(String str) {
        this.parentId = str;
    }

    public void setOnTwoClickListener(OnTwoClickListener onTwoClickListener2) {
        this.onTwoClickListener = onTwoClickListener2;
    }

    public void setUserId(int i) {
        this.userId = i;
    }

    public NovelCommentVerticalLayout(Context context) {
        super(context);
        init();
    }

    public NovelCommentVerticalLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    public NovelCommentVerticalLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init();
    }

    private void init() {
        setOrientation(1);
        this.mCommentVerticalSpace = UiUtils.dp2px(2.0f);
        setOnHierarchyChangeListener(this);
    }

    public void addCommentsWithLimit(List<NovelReplyBean> list, int i, boolean z) {
        if (list != null) {
            this.replyList = list;
            int childCount = getChildCount();
            if (!z && childCount > 0) {
                removeViewsInLayout(0, childCount);
            }
            int min = Math.min(i, list.size());
            int i2 = 0;
            while (i2 < min) {
                View childAt = i2 < childCount ? getChildAt(i2) : null;
                NovelReplyBean novelReplyBean = list.get(i2);
                if (childAt == null) {
                    addViewInLayout(makeCommentItemView(novelReplyBean, i2), i2, generateMarginLayoutParams(i2), true);
                } else {
                    addCommentItemView(childAt, novelReplyBean, i2);
                }
                i2++;
            }
            requestLayout();
        }
    }

    private LinearLayout.LayoutParams generateMarginLayoutParams(int i) {
        if (this.mLayoutParams == null) {
            this.mLayoutParams = new LinearLayout.LayoutParams(-1, -2);
        }
        if (this.replyList != null && i > 0) {
            this.mLayoutParams.topMargin = (int) (((float) this.mCommentVerticalSpace) * 1.2f);
        }
        return this.mLayoutParams;
    }

    private void updateCommentData(View view, NovelReplyBean novelReplyBean, int i) {
        bindViewData(view, novelReplyBean);
    }

    private void addCommentItemView(View view, NovelReplyBean novelReplyBean, int i) {
        addViewInLayout(makeCommentItemView(novelReplyBean, i), i, generateMarginLayoutParams(i), true);
    }

    private View makeCommentItemView(NovelReplyBean novelReplyBean, int i) {
        return makeContentView(novelReplyBean, i);
    }

    private View makeContentView(final NovelReplyBean novelReplyBean, int i) {
        View inflate = View.inflate(getContext(), R.layout.comment_item_two, null);
        inflate.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentVerticalLayout.View$OnClickListenerC32001 */

            public void onClick(View view) {
                if (NovelCommentVerticalLayout.this.onTwoClickListener != null) {
                    NovelCommentVerticalLayout.this.onTwoClickListener.onTwoClick(novelReplyBean, NovelCommentVerticalLayout.this.parentId);
                }
            }
        });
        bindViewData(inflate, novelReplyBean);
        return inflate;
    }

    private void bindViewData(View view, final NovelReplyBean novelReplyBean) {
        String str;
        TextView textView = (TextView) view.findViewById(R.id.tv_author);
        TextView textView2 = (TextView) view.findViewById(R.id.tv_content);
        TextView textView3 = (TextView) view.findViewById(R.id.tv_time);
        LikeButton likeButton = (LikeButton) view.findViewById(R.id.bt_isLike);
        final TextView textView4 = (TextView) view.findViewById(R.id.tv_like_num);
        GlideUtils.loadPicHead(SpUtils.getInstance().getString("domain") + novelReplyBean.getUserLogo(), (CircleImageView) view.findViewById(R.id.img_head));
        ((TextView) view.findViewById(R.id.tv_name)).setText(novelReplyBean.getUserNickName());
        if (novelReplyBean.getUserId() == 0) {
            textView2.setText(novelReplyBean.getContent());
        } else {
            if (novelReplyBean.getReplyUserNickName() != null) {
                str = "回复  " + novelReplyBean.getReplyUserNickName() + ":  ";
            } else {
                str = "";
            }
            SpannableString spannableString = new SpannableString(str + novelReplyBean.getContent());
            if (!str.equals("")) {
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffff")), 4, novelReplyBean.getUserNickName().length() + 4, 33);
            }
            textView2.setText(spannableString);
        }
        textView3.setText(TimeUtils.utc2Common(novelReplyBean.getCreatedAt()));
        textView4.setText(UiUtils.num2str(novelReplyBean.getLikeNum()));
        boolean z = false;
        if (this.userId == novelReplyBean.getUserId()) {
            textView.setVisibility(0);
        } else {
            textView.setVisibility(8);
        }
        final int userId2 = SpUtils.getInstance().getUserInfo().getUserId();
        if (novelReplyBean.getLikeUserIds() != null) {
            z = novelReplyBean.getLikeUserIds().contains(Integer.valueOf(userId2));
        }
        if (z) {
            textView4.setTextColor(BaseSdk.getContext().getResources().getColor(R.color.E24339));
        } else {
            textView4.setTextColor(BaseSdk.getContext().getResources().getColor(R.color.white));
        }
        likeButton.setLiked(Boolean.valueOf(z));
        likeButton.setOnLikeListener(new OnLikeListener() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentVerticalLayout.C32012 */

            @Override // com.like.OnLikeListener
            public void liked(LikeButton likeButton) {
                if (ButtonUtil.isFastDoubleClick(1000)) {
                    likeButton.setLiked(false);
                    return;
                }
                List<Integer> likeUserIds = novelReplyBean.getLikeUserIds();
                if (likeUserIds == null) {
                    likeUserIds = new ArrayList<>();
                }
                likeUserIds.add(Integer.valueOf(userId2));
                novelReplyBean.setLikeUserIds(likeUserIds);
                NovelReplyBean novelReplyBean = novelReplyBean;
                novelReplyBean.setLikeNum(novelReplyBean.getLikeNum() + 1);
                textView4.setText(UiUtils.num2str(novelReplyBean.getLikeNum()));
                textView4.setTextColor(BaseSdk.getContext().getResources().getColor(R.color.E24339));
                NovelCommentHttpUtils.replyLike(novelReplyBean);
            }

            @Override // com.like.OnLikeListener
            public void unLiked(LikeButton likeButton) {
                if (ButtonUtil.isFastDoubleClick(1000)) {
                    likeButton.setLiked(true);
                    return;
                }
                if (novelReplyBean.getLikeNum() >= 1) {
                    NovelReplyBean novelReplyBean = novelReplyBean;
                    novelReplyBean.setLikeNum(novelReplyBean.getLikeNum() - 1);
                }
                List<Integer> likeUserIds = novelReplyBean.getLikeUserIds();
                if (likeUserIds != null) {
                    for (int i = 0; i < likeUserIds.size(); i++) {
                        if (userId2 == likeUserIds.get(i).intValue()) {
                            likeUserIds.remove(i);
                        }
                    }
                }
                textView4.setText(UiUtils.num2str(novelReplyBean.getLikeNum()));
                textView4.setTextColor(BaseSdk.getContext().getResources().getColor(R.color.white));
                NovelCommentHttpUtils.replyUnlike(novelReplyBean);
            }
        });
    }
}
